package club.weiyuanstudio.android.app.message_station.ui.message

import androidx.recyclerview.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import club.weiyuanstudio.android.app.message_station.R
import club.weiyuanstudio.android.app.message_station.api.data.Message

/**
 * [RecyclerView.Adapter] that can display a [DummyItem].
 * TODO: Replace the implementation with code for your data type.
 */
class MessageRecyclerViewAdapter(
        private val values: Array<Message>)
    : RecyclerView.Adapter<MessageRecyclerViewAdapter.ViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context)
                .inflate(R.layout.message_card_item, parent, false)
        return ViewHolder(view)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val item = values[position]
        holder.fromNumber.text = item.senderNumber
        holder.receiveDateTime.text = item.dateTime.toString()
        holder.messageContent.text = item.messageContent
    }

    override fun getItemCount(): Int = values.size

    inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val fromNumber: TextView = view.findViewById(R.id.textViewFromNumber)
        val receiveDateTime: TextView = view.findViewById(R.id.textViewReceiveDateTime)
        val messageContent: TextView = view.findViewById(R.id.textViewMessageContent)

        override fun toString(): String {
            return super.toString() + " '" + messageContent.text + "'"
        }
    }
}